package com.gujiangbo.future;

import java.util.concurrent.TimeUnit;

public class FutureServiceTest {

    public static void main(String[] args) throws InterruptedException {
        //  unReturn();
        hasReturn();

    }

    /**
     * 测试无返回
     */
    public static void unReturn() throws InterruptedException {
        FutureService<Void, Void> service = FutureService.newService();
        Future<?> future = service.submit(() -> {

            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I finish done .");
        });
        // get方法会使当前线程进入阻塞状态
        future.get();
    }

    /**
     * 又返回
     *
     * @throws InterruptedException
     */
    public static void hasReturn() throws InterruptedException {
        FutureService<String, Integer> service = FutureService.newService();
        Future<Integer> future = service.submit(input -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return input.length();
        }, "333333");

        //get方法获取当前线程的计算结果
        System.out.println(future.get());
    }
}
